package com.doyens.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.doyens.reggie.domain.Category;
import com.doyens.reggie.domain.Dish;
import com.doyens.reggie.domain.Setmeal;
import com.doyens.reggie.exception.CustomException;
import com.doyens.reggie.mapper.CategoryMapper;
import com.doyens.reggie.service.CategoryService;
import com.doyens.reggie.service.DishService;
import com.doyens.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;



    @Override
    public void mydeleteCategory(Long id) {
        // 先查询dish表
        int count = dishService.count(new LambdaQueryWrapper<Dish>().eq(Dish::getCategoryId, id));
        if(count > 0){
            // 告诉前端  当前分类不能删除  人为的异常
            throw new CustomException("当前菜系还有其他的菜品，不能删除");
        }
        // 查询setmeal
        int count2 = setmealService.count(new LambdaQueryWrapper<Setmeal>().eq(Setmeal::getCategoryId, id));
        if(count2 > 0){
            // 告诉前端  当前分类不能删除
            throw new CustomException("当前套餐还有其他的套餐，不能删除");
        }
        // 就可以删除
        this.removeById(id);
    }
}
